﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Core.Helpers
{
    public class Logs
    {
        public static string path = AppDomain.CurrentDomain.BaseDirectory + "logs";
        public static object loglocker = new object();
        protected static void WriteLog(string type,string content)
        {
            lock(loglocker)
            {
                if(!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);//创建目录
                }

                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                string filename = path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                StreamWriter sw = File.AppendText(filename);
                string write_content = time + " " + type + ":" + content;
                sw.WriteLine(write_content);
                sw.Close();
            }
        }
        public static void Debug(string content)
        {
            WriteLog("DEBUG", content);
        }

        public static void Info(string content)
        {
            WriteLog("INFO", content);
        }
        public static void Error(string content)
        {
            WriteLog("ERROR", content);
        }
        public static void Error(Exception ex)
        {
            WriteLog("ERROR", ex.Message+ex.StackTrace);
        }
    }
}
